namespace :db do

  @admin_email= 'root@163.com'
  task admin_init: :permissions_init do
    _email= @admin_email
    user = Admin::User.find_by_email(_email)
    unless user.nil?
      unless user.destroy
        puts '对不起，原有数据清楚失败。'
      end
    end
    user= Admin::User.new
    user.email=_email
    user.admin=true
    user.password='wangzhihua'
    #user.permissions <<   permission
    if user.save!
      puts 'admin账户初始化完成'
    end
  end
  @ordinary_email='wzhjbfq3@163.com'
  @tourist_email= 'tourist@163.com'
  task init_account: :admin_init do
    if Admin::User.create(email: @ordinary_email, password: 'wzhjbfq3', admin: false)
      puts "普通用户#@ordinary_email 添加完成"
    end
    if Admin::User.create(email: @tourist_email, password: 'tourist', admin: false)
      puts "系统用户#@tourist_email 添加完成！"
    end
  end

  task init_permission_actions: :environment do
    action_array=[:create, :update, :read, :destroy, :manage,:registration, :show_about, :login_function ]
    action_array.each do |action_name|
      if PermissionAction.create(action_name: action_name.to_s)
        puts "基础权限行为　#{action_name}　添加成功"
      end

    end
  end
  #权限－资源表初始化
  task permissions_init: :init_permission_actions do
    permission_actions= PermissionAction.all
    permission_actions.each do |pa|
      if pa.action_name=='manage'
        #最高权限
       if Permission.create(subject_class: 'all',permission_action: pa)
         puts "超级用户权限添加完成！"
       end

      elsif pa.action_name=='show_about'
        if Permission.create(subject_class: 'Admin::User', permission_action: pa)
          puts "赋予所有的 Admin::User 执行　#{pa.action_name} 的权限！"
        end
      else
        #对所有用户的create / destroy / update / read
        #can [:create, :destroy, :update, :read], Admin::User
        if Permission.create(subject_class: 'Admin::User', permission_action: pa)
          puts "添加权限：Admin::User #{pa.action_name}"
        end
        #can [:create, :destroy, :update, :read], Post
        Permission.create(subject_class: 'Post', permission_action: pa)
      Permission.create(subject_class: 'BlogSort', permission_action: pa)

      end

    end

  end


  task ability_init: :init_account do
    ability_init_is_admin
    ability_init_is_tourist
    ability_init_is_ordinary
  end
  #直接将权限初始化给超级用户
  def ability_init_is_admin
    pa= PermissionAction.find_by_action_name('manage')
    permission = Permission.find_by(permission_action: pa, subject_class: 'all')
    user = Admin::User.find_by_email(@admin_email)
    user.permissions <<   permission
  end
  #直接将权限初始化给游客用户
  def ability_init_is_tourist
    # can [:create,:read], Post
    # can [:update, :destroy], Post, admin_user_id= current_user.id
    # can [:update, :destroy, :create, :read], BlogSort, admin_user_id= current_user.id
    #
    pa= PermissionAction.find_by_action_name('create')
    login_function= PermissionAction.find_by_action_name('login_function')
    registration= PermissionAction.find_by_action_name('registration')
    user = Admin::User.find_by_email(@tourist_email)
    permission = Permission.find_by(permission_action: pa, subject_class: 'Post')
    login_function_permission = Permission.find_by(permission_action: login_function, subject_class: 'Admin::User')
    registration_permission= Permission.find_by(permission_action: registration, subject_class: 'Admin::User')
    user.permissions <<   [permission,login_function_permission, registration_permission]
    ability_permission :show_about, 'Admin::User', user

  end

  def ability_init_is_ordinary
    @user = Admin::User.find_by_email(@ordinary_email)
    ability_permission :show_about, 'Admin::User', @user
  end

  def ability_permission(action,subject_class, user=@user)
    create_action= PermissionAction.find_by_action_name(action.to_s)
    destroy_p= Permission.find_by(permission_action: create_action, subject_class: subject_class.to_s)
    user.permissions << destroy_p
  end






  task role_init: :environment do
    init_add_role
    init_ability_is_role
    user = Admin::User.find_by_email(@admin_email)

  end
  #添加用户角色
  def init_add_role
    Role.create(name: 'root',nick_name: '超级管理员')
    Role.create(name: 'tourist',nick_name: '游客')
    Role.create(name: 'ordinary',nick_name: '普通用户')
  end

  #初始化授权给角色
  def init_ability_is_role
    @permissions= Permission.all
    @permissions.each do |p|
      Role.create(name: 'root', resource_type: :Permission, resource_id: p.id)
    end
  end


end
